package model

import (
	"ginBlog/utils/errmsg"
	"github.com/jinzhu/gorm"
)

type Article struct {
	// 设置 Cid 为 外键
	Category Category `gorm:"foreignkey:Cid"`
	gorm.Model
	Title string `gorm:"type:varchar(100);not null" json:"title"`
	// 利用 Cid 与 Category 模型相互关联，在执行查询的时候，能够达到Article、Category关联查询的功能
	Cid     int    `gorm:"type:int;not null" json:"cid"`
	Desc    string `gorm:"type:varchar(500)" json:"desc"`
	Content string `gorm:"type:longtext" json:"content"`
	Img     string `gorm:"type:varchar(100)" json:"img"`
}

// 查询文章是否存在
func CheckArticle(title string) int {
	var article Article
	db.Select("id").Where("title = ?", title).First(&article)
	if article.ID > 0 {
		return errmsg.ERROR_ARTICLENAME_USED
	}
	return errmsg.SUCCESS
}

// 更新文章查询
func CheckUpArt(id int, title string) (code int) {
	var art Article
	// 查询判刑是否有此id有即更新
	db.Select("id, title").Where("title = ?", title).First(&art)
	if art.ID == uint(id) {
		return errmsg.SUCCESS
	}
	if art.ID > 0 {
		return errmsg.ERROR_ARTICLENAME_USED
	}
	return errmsg.SUCCESS
}

// 创建文章
func CreateArticle(data *Article) int {
	err = db.Create(&data).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 根据分类获取文章列表
func GetArticlesByCategory(id int, pageSize int, pageNum int) ([]Article, int, int) {
	var artBycates []Article
	var total int
	err := db.Preload("Category").Limit(pageSize).Offset((pageNum-1)*pageSize).Where("cid = ?", id).Find(&artBycates).Count(&total).Error

	if err != nil {
		return nil, errmsg.ERROR_USER_NOT_EXIST, 0
	}
	return artBycates, errmsg.SUCCESS, total
}

// 获取单个文章
func GetArticleInfo(id int) (Article, int) {
	var article Article
	err := db.Preload("Category").Where("id = ?", id).First(&article).Error
	if err != nil {
		return article, errmsg.ERROR_ARTICLENAME_USED
	}

	return article, errmsg.SUCCESS
}

// 获取文章列表
func GetArticles(title string, pageSize int, pageNum int) ([]Article, int, int) {
	var articleList []Article
	var err error
	var total int
	if title == "" {
		err = db.Order("updated_at DESC").Preload("Category").Find(&articleList).Limit(pageSize).Offset((pageNum - 1) * pageSize).Error
		// 单独计数
		db.Model(&articleList).Count(&total)
		if err != nil && err != gorm.ErrRecordNotFound {
			return nil, errmsg.ERROR, 0
		}
		return articleList, errmsg.SUCCESS, total
	}

	err = db.Order("updated_at DESC").Preload("Category").Where("title LIKE ?", title+"%").Find(&articleList).Count(&total).Limit(pageSize).Offset((pageNum - 1) * pageSize).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, errmsg.ERROR, 0
	}
	return articleList, errmsg.SUCCESS, total
}

// 编辑文章
func EditArticle(id int, data *Article) int {
	var article Article
	var maps = make(map[string]interface{})
	maps["title"] = data.Title
	maps["cid"] = data.Cid
	maps["desc"] = data.Desc
	maps["content"] = data.Content
	maps["img"] = data.Img

	err = db.Model(&article).Where("id = ?", id).Update(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS

}

// 删除文章
func DeleteArticle(id int) int {
	var artcile Article
	err = db.Where("id = ?", id).Delete(&artcile).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}
